//
//  UIOrientationBlackBox.m
//  UI_Test_Frame
//
//  Created by tdh1 on 01/09/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "UIOrientationBlackBox.h"
#import "AccelerometerFilter.h"

#define kUpdateFrequency 1.0

@implementation UIOrientationBlackBox

@synthesize x,y,z,longitude,latitude;

static UIOrientationBlackBox *sharedBlackBox = nil;

+ (UIOrientationBlackBox *)sharedBlackBox
{
	if (sharedBlackBox == nil){
		sharedBlackBox = [[UIOrientationBlackBox alloc] init];
	}
	
	return sharedBlackBox;
}

- (id) init
{
	self = [super init];
	if (self != nil) {
		
		self.longitude = 0;
		self.latitude = 0;
		
		locationManager = [[CLLocationManager alloc] init];
		locationManager.delegate = self;
		locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
		[locationManager startUpdatingLocation];
		
		[self changeFilter:[LowpassFilter class]];
		[[UIAccelerometer sharedAccelerometer] setUpdateInterval:1.0 / kUpdateFrequency];
		[[UIAccelerometer sharedAccelerometer] setDelegate:self];
	}
	return self;
}

-(void)changeFilter:(Class)filterClass
{
	// Ensure that the new filter class is different from the current one...
	if(filterClass != [filter class])
	{
		[filter release];
		filter = [[filterClass alloc] initWithSampleRate:kUpdateFrequency cutoffFrequency:5.0];
		filter.adaptive = YES;
	}
}

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration 
{
//	NSLog(@"x:%f y:%f z:%f",filter.x, filter.y,filter.z);
	[filter addAcceleration:acceleration];
	self.x = filter.x;
	self.y = filter.y;
	self.z = filter.z;
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{
//	NSLog(@"Got Location Data");
	self.latitude = newLocation.coordinate.latitude;
	self.longitude = newLocation.coordinate.longitude;
}

- (void)recordChanges
{

}

@end
